﻿// JavaScript Document
var oldUserTypeID = -1;
var typeIDString = "";
function Init()
{
	//城市
	var sTemp = document.getElementById("hidCityID").value;			  
	sTemp = sTemp.substr(0,2);
	var obj1 = document.getElementById("selProvince");
	var obj2 = document.getElementById("selCity");
	refresh_obj(obj1, "UUD_PROVINCE",sTemp);		   						  
	select_obj(obj2, "UUD_CITY", obj1.value, document.getElementById("hidCityID").value);
	
	//生日
	birthday = document.getElementById("hidBirthday").value;	
	init_calendar(1,birthday);
	if( birthday != null && birthday != "" ){		     
		document.getElementById("Year_1").value = birthday.substring(0,4);
		document.getElementById("Month_1").value = birthday.substring(5,7);
		document.getElementById("Day_1").value = birthday.substring(8,10);
	}else{
		document.getElementById("Year_1").value = "1980";
		document.getElementById("Month_1").value = "01";
		document.getElementById("Day_1").value = "01";
	} 

	//性别
	var isex = document.getElementById("hidSex").value;			
	sTemp = "Radio" + document.getElementById("hidSex").value;
	document.getElementById(sTemp).checked = true;
}
function refresh_obj(obj, arr, value)
{
	if (!value) value = "0";
	
	obj_opt = eval(obj);
	obj_arr = eval(arr);
	obj_opt.options.length = 1;
	obj_opt.options[0] = new Option("--请选择--", "0");
	
	for (i = 1; i <= obj_arr.length / 2; i ++)
	{
		obj_opt.options[i] = new Option(obj_arr[(i - 1) * 2 + 1], obj_arr[(i - 1) * 2]);
		if (value == obj_arr[(i - 1) * 2])
		{
			obj_opt.options[i].selected = true;
		}
	}
}
function select_obj(obj, arr, filter, value)
{
	if (!value) value = "0";
	
	obj_opt = eval(obj);
	obj_arr = eval(arr);
	obj_opt.options.length = 1;
	obj_opt.options[0] = new Option("--请选择--", "0");
	
	j = 1;
	for (i = 0; i < obj_arr.length / 2; i ++)
	{
		if (obj_arr[i * 2].substring(0, 2) == filter)
		{
		   obj_opt.options[j] = new Option(obj_arr[i * 2 + 1], obj_arr[i * 2]);
		   if (value == obj_arr[i * 2]) obj_opt.options[j].selected = true;
		   j ++;
		}
	}
}
function init_calendar(label, date)
{
	if (null == date || 0 == date.length) init_calendar(label, '0', '0', '0');
	
	var year = date.split(new RegExp('[-.年月日]'))[0];
	var month = date.split(new RegExp('[-.年月日]'))[1];
	var day = date.split(new RegExp('[-.年月日]'))[2];
	init_year(label, year);
	init_month(label, month);
	init_day(label, day);
}
//初始化年下拉列表框
function init_year(label, year)
{
	var today = new Date()
	var current_year = today.getFullYear();
	Year = eval(document.getElementById('Year_' + label));
	Year.options.length = 1;
	Year.options[0] = new Option('--请选择--', '0');
	for (i = current_year, j = 1; i >= 1900; i --, j ++)
	{
		Year.options[j] = new Option(i, i);		
		if (year == i) Year.options[j].selected = true;
	}
}
//初始化月的下拉列表框
function init_month(label, month)
{
	Month = eval(document.getElementById('Month_' + label));
	Month.options.length = 1;
	Month.options[0] = new Option('--请选择--', '0');
	for (i = 1; i <= 12; i ++)
	{
		if ( i < 10 )
		{
			j = "0" + i;
		}
		else
		{
			j = i;
		}
		Month.options[i] = new Option(j, j);		
		if (month == j) Month.options[i].selected = true;
	}
}
//初始化天的下拉列表框
function init_day(label, day)
{
	Day = eval(document.getElementById('Day_' + label));
	Day.options.length = 1;
	Day.options[0] = new Option('--请选择--', '0');
	var year = document.getElementById('Year_' + label).value;
	var month = document.getElementById('Month_' + label).value;
	var day_count = getdays(year, month);
	day_count = 0 == day_count? 31: day_count;	
	for (i = 1; i <= day_count; i ++)
	{
		if ( i < 10 )
		{
			j = "0" + i;
		}
		else
		{
			j = i;
		}
		Day.options[i] = new Option(j, j);		
		if (day == j) Day.options[i].selected = true;
	}
}
//根据输入的年月获取月的天数
function getdays(year, month)
{
	if (1 > month || 12 < month) return 0;
	var days_in_month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)))
	{
		days_in_month.splice(1, 1, 29);
	}
	else
	{
		days_in_month.splice(1, 1, 28);
	}
	return days_in_month[month - 1];
}
function InitDate(){
	var day = new Date();
	var year = day.getYear();
	for(i=1930;i<=year;i++)
		document.getElementById("selYear").options[i-1930]= new Option(i,i);
	for(i=1;i<=12;i++){
		j=i;
		if(i<10)
			j="0"+i;
		document.getElementById("selMonth").options[i-1]= new Option(j,j);
	}
	for(i=1;i<=31;i++){
		j=i;
		if(i<10)
			j="0"+i;
		document.getElementById("selDay").options[i-1]= new Option(j,j);
	}
}
function changeProvince()
{
	var obj1= document.getElementById("selProvince");
	var obj2= document.getElementById("selCity");
	select_obj(obj2, "UUD_CITY", obj1.value, "");
	if(obj2.options.length>1)
		obj2.selectedIndex=1;
	document.getElementById("hidCityID").value = document.getElementById("selCity").value;
}
function changeCity()
{
	document.getElementById("hidCityID").value = document.getElementById("selCity").value;
}
function changeBirther()
{
	refresh_day(1);
	document.getElementById("hidBirthday").value = document.getElementById("Year_1").value + "-" + document.getElementById("Month_1").value + "-" + document.getElementById("Day_1").value;
}
//更新天数的下拉列表框
function refresh_day(label)
{
	var day = document.getElementById('Day_' + label).value;
	
	init_day(label, day);
}

function SexChanged(v)
{
	document.getElementById("hidSex").value = v;
}

//判断是否为中文
function isChinese( str )
{
	var lst = /[^\u4E00-\u9FA5]/; 
	return !lst.test(str);      
}
//返回字符串的长度
function GetStringLength( str )
{
	var strLength=0;
	for ( i = 0; i < str.length; i ++ )
	{
		if ( isChinese( str.charAt( i ) ) == true )
		{
			strLength = strLength + 2;
		}
		else
		{
			strLength = strLength + 1;
		}
	}
	return strLength;
}
//去掉字符串空格
String.prototype.Trim = function() 
{ 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 

//检查昵称是否符合规定
function CheckNickName( bool )
{
	var newNickName = document.getElementById("txtNickname").value;
	if ( newNickName.Trim() == "" )
	{
		document.getElementById("nickNameErr").innerHTML = "<span class=\"error\">昵称不能为空！</span>";
		return false;
	}
	else
	{
		if ( GetStringLength( document.getElementById("txtNickname").value ) > 20 )
		{
			document.getElementById("nickNameErr").innerHTML = "<span class=\"error\">昵称的长度不能超过20个字符!</span>";
			return false; 
		}
		if ( bool )
		{
			var res = modifyinfo.CheckNickName( document.getElementById("hidUserID").value, newNickName );
			if ( res.error != null )
			{
				alert(res.error);
			}
			else
			{
				if ( res.value == 1 )
				{
					document.getElementById("nickNameErr").innerHTML = "输入的昵称可以正常使用！";
					return true;
				}
				else
				{
					document.getElementById("nickNameErr").innerHTML = "<span class=\"error\">输入的昵称已经有用户使用，请重新输入！</span>";
					return false;
				}
			}
		}
		else
		{
			return true;	
		}
	}
}

//检查邮箱是否符合规定
function CheckEMail( bool )
{
	var newEmail = document.getElementById("txtMail").value;
	if ( newEmail.Trim() == "" )
	{
		document.getElementById("emailErr").innerHTML = "<span class=\"error\">邮箱不能为空！</span>";
		return false;
	}
	else
	{
		var filter=/^\s*([A-Za-z0-9_-]+(\.\w+)*@(\w+\.)+\w{2,3})\s*$/; 
		if(!filter.test(newEmail))
		{ 
			document.getElementById("emailErr").innerHTML = "<span class=\"error\">邮箱格式错误或含有非法字符!</span>";
			return false; 
		}
		if ( bool )
		{
			document.getElementById("emailErr").innerHTML = "输入的邮箱可以正常使用！";
			return true; 
		}
		else
		{
			return true; 
		}
	}
}

//检查真实姓名是否符合规范
function CheckRealName( bool )
{
	if ( document.getElementById("popgroupInfo").style.display == "block" || document.getElementById("popgroupInfo").style.display == "" )
	{
		if ( document.getElementById("txtRealname").value.Trim() == "" )
		{
			document.getElementById("realNameErr").innerHTML = "<span class=\"error\">姓名不能为空！</span>";
			return false;
		}
		else
		{
			var filter1= /[^\u4E00-\u9FA5]/; 
			if( !filter1.test(document.getElementById("txtRealname").value) )
			{ 
				if ( document.getElementById("txtRealname").value.length > 4 || document.getElementById("txtRealname").value.length < 2 )
				{
					document.getElementById("realNameErr").innerHTML = "<span class=\"error\">中文名字的长度为2-4个字!</span>";
					return false; 
				}
			}
			else
			{
				if ( GetStringLength( document.getElementById("txtRealname").value ) > 20 || GetStringLength( document.getElementById("txtRealname").value ) < 4 )
				{
					document.getElementById("realNameErr").innerHTML = "<span class=\"error\">名字的长度为4-20个字符!</span>";
					return false; 
				}
			}
		}
		if (bool)
		{
			document.getElementById("realNameErr").innerHTML = " 正确！";
			return true;
		}
		else
		{
			return true;
		}
	}
	return true;
}

//判断空间名称是否为空
function CheckSpaceName(bool)
{
	if ( document.getElementById("txtSpaceName").value.Trim() == "" )
	{
		document.getElementById("spaceNameErr").innerHTML = "<span class=\"error\">空间名称不能为空！</span>";
		return false;
	}
	if (bool)
	{
		document.getElementById("spaceNameErr").innerHTML = "输入的空间名称正确！";
		return true;
	}
	else
	{
		return true; 
	}
}
function checkInput()
{
	//判断昵称
	if ( !CheckNickName(false) )
	{
		document.getElementById("txtNickname").focus();
		document.getElementById("txtNickname").select();
		return false;
	}
	//检查邮箱是否符合规定
	else if ( !CheckEMail(false) )
	{
		document.getElementById("txtMail").focus();
		document.getElementById("txtMail").select();
		return false;
	}
	/*else if ( !CheckSpaceName(false) )
	{
		document.getElementById("txtSpaceName").focus();
		document.getElementById("txtSpaceName").select();
		return false;
	}*/
	return true;
}
//判断修改资料提交时，是否满足条件
function ModifyInfo()
{
	
	if ( checkInput()==true )
	{
		var nickName = document.getElementById("txtNickname").value;
		var email = document.getElementById("txtMail").value;
		//var spaceName = document.getElementById("txtSpaceName").value;
		var spaceName = "";
		var sex = document.getElementById("hidSex").value;
		var birthday = document.getElementById("hidBirthday").value;
		var tempbirthday = birthday.split('-');
		for( var i = 0 ; i < tempbirthday.length; i ++ )
		{
			if (tempbirthday[i]=="0")
			{
				document.getElementById("birthdayErr").innerHTML = "<span class=\"error\">错误的日期格式</span>";
				return false;
			}
		}
		var city = document.getElementById("hidCityID").value;
		//var realName = document.getElementById("txtRealname").value;
		//var qq = document.getElementById("txtQQ").value;
		//var selfInfo = document.getElementById("txtSelfIntro").value;
		
		var res = modifyinfo.ModifyUserInfo( document.getElementById("hidUserID").value, nickName, spaceName, email, sex, birthday, city );
		if ( null != res.error )
		{
			alert ( res.error ) ;
		}
		else
		{
			if ( res.value == -1 )
			{
				document.getElementById("nickNameErr").innerHTML = "<span class=\"error\">会员不存在！</span>";
				return;
			}
			else if ( res.value == -2 )
			{
				document.getElementById("nickNameErr").innerHTML = "<span class=\"error\">输入的昵称已经有用户使用，请重新输入！</span>";
				return;
			}
			else if ( res.value == -3 )
			{
				document.getElementById("emailErr").innerHTML = "<span class=\"error\">输入的邮箱已经有用户使用，请重新输入！</span>";
				return;
			}
			else if ( res.value == -4 )
			{
				Pui.manage.MsgDiv(1,"修改失败！","修改失败，请稍后再试！");
				return;
			}
			else
			{
				Pui.manage.MsgDiv(1,"成功修改个人资料！","成功修改个人资料！");
			}	
		}
	}
}

//判断申请爆爆团时，是否有选择类
function CheckTypeSelect()
{
	var typeIDString = "";
	var tempPopID = document.getElementsByName("popGroupTypeID");

	var tempErr = "";
	for ( var i = 0; i < tempPopID.length; i ++ )
	{
		if ( tempPopID[i].checked )
		{
			tempErr += tempPopID[i].value + ",";
		}
	}
	if ( tempErr == "" )	
	{
		document.getElementById("typeErr").innerHTML = "<span class=\"error\">请选择你所要加入的爆爆团分类！</span>";
		return false;	
	}	
	
	for ( var j = 0; j < tempPopID.length; j ++ )
	{
		if ( tempPopID[j].checked )
		{
			var checkedValue = "";
			var tempTypeID = document.getElementsByName("userTypeID"+tempPopID[j].value);
			for ( var k = 0; k < tempTypeID.length; k ++ )
			{
				if ( tempTypeID[k].checked )
				{
					checkedValue += tempTypeID[k].value + ",";
				}
			}
			if (checkedValue == "")
			{ 
				document.getElementById("typeErr").innerHTML = "<span class=\"error\">其中有一个大类，你没有选择任何项！</span>"; 
				return; 
			}
		}
	}
	return true;
}
//申请爆爆团判断
function CheckJoinPop()
{
	if ( document.getElementById("popgroupInfo").style.display == "none" )
	{
		document.getElementById("popErr").innerHTML = "<span class=\"error\">请点击“加入爆爆团”完善信息！</span>";
		return false;
	}
	//判断昵称
	if ( !CheckNickName(false) )
	{
		document.getElementById("txtNickname").focus();
		document.getElementById("txtNickname").select();
	}
	//检查邮箱是否符合规定
	else if ( !CheckEMail(false) )
	{
		document.getElementById("txtMail").focus();
		document.getElementById("txtMail").select();
	}
	//检查真实姓名是否符合规范
	else if ( !CheckRealName() )
	{
		document.getElementById("txtRealname").focus();
		document.getElementById("txtRealname").select();
	}
	//判断QQ是否正确
	else if ( !CheckQQ() )
	{
		document.getElementById("txtQQ").focus();
		document.getElementById("txtQQ").select();
	}
	//判断空间名称是否正确
	else if ( !CheckSpaceName() )
	{
		document.getElementById("txtSpaceName").focus();
		document.getElementById("txtSpaceName").select();
	}	
	else
	{
		if ( CheckTypeSelect() )
		{
			var nickName = document.getElementById("txtNickname").value;
			var email = document.getElementById("txtMail").value;
			var spaceName = document.getElementById("txtSpaceName").value;
			var sex = document.getElementById("hidSex").value;
			var birthday = document.getElementById("hidBirthday").value;
			var city = document.getElementById("hidCityID").value;
			var realName = document.getElementById("txtRealname").value;
			var qq = document.getElementById("txtQQ").value;
			var selfInfo = document.getElementById("txtSelfIntro").value;
			
			var typeIDString = "";
			var tempPopID = document.getElementsByName("popGroupTypeID");
			for ( var j = 0; j < tempPopID.length; j ++ )
			{
				if ( tempPopID[j].checked )
				{
					typeIDString += tempPopID[j].value + ",";
					var tempTypeID = document.getElementsByName("userTypeID"+tempPopID[j].value);
					for ( var k = 0; k < tempTypeID.length; k ++ )
					{
						if ( tempTypeID[k].checked )
						{
							typeIDString += tempTypeID[k].value + ",";
						}
						if ( k == tempTypeID.length - 1 )
						{
							typeIDString += "|";
						}
					}
				}
			}
		
			var res = info.JoinPopGroup( userID, nickName, spaceName, email, sex, birthday, city, realName, qq, selfInfo, typeIDString );
			if ( null != res.error )
			{
				alert ( res.error ) ;
			}
			else
			{
				if ( res.value == -1 )
				{
					document.getElementById("nickNameErr").innerHTML = "<span class=\"error\">会员不存在！</span>";
					return;
				}
				else if ( res.value == -2 )
				{
					document.getElementById("nickNameErr").innerHTML = "<span class=\"error\">输入的昵称已经有用户使用，请重新输入！</span>";
					return;
				}
				else if ( res.value == -3 )
				{
					document.getElementById("emailErr").innerHTML = "<span class=\"error\">输入的邮箱已经有用户使用，请重新输入！</span>";
					return;
				}
				else if ( res.value == 0 )
				{
					document.getElementById("typeErr").innerHTML = "<span class=\"error\">加入爆爆团时出错，请重试！</span>";
					return;
				}
				else if ( res.value == -4 )
				{
					alert("数据操作异常");
					return;
				}
				else
				{
					alert( "成功加入爆爆团及修改个人资料！" );
					SetTypesDisabled();
				}
			}
		}
	}
}

//申请加入爆爆团的时候判断是否有上传或录制过视频
function CheckVdieoNum()
{
	var ret = info.CheckVdieoNum( userID );
	if( ret.value <= 0 )
	{
		document.getElementById("typeErr").innerHTML = "<span class=\"error\">您还未上传视频，请先上传视频！</span>";
		return false;
	}
	return true;
}


function GetSmallUserType( popids, stypeids )
{
	if ( popids != "" )
	{
		var tempPopID = popids.split(',');
		for ( var i = 0; i < tempPopID.length - 1; i ++ )
		{
			var retemp = info.GetUserTypeHtml( tempPopID[i], stypeids );
			if( retemp.error != null )
			{
				alert(retemp.error);
				return;
			}
			else
			{
				document.getElementById("typeErr").innerHTML = "";
				document.getElementById("userType"+tempPopID[i]).innerHTML =retemp.value;
			}
		}
	}
}
